package com.joker.controller;

import com.joker.bean.Course;
import com.joker.bean.Notice;
import com.joker.bean.Student;
import com.joker.bean.User;
import com.joker.service.CourseService;
import com.joker.service.NoticeService;
import com.joker.service.StudentService;
import com.joker.service.UserService;
import com.joker.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private NoticeService noticeService;

    @RequestMapping(value = "/register")
    public String register(String newName,String newPassword){
        int i = userService.addUser(new User(newName, "管理员", newPassword, "1"));
        if(i>0){
            System.out.println("注册成功");
        }
        return "../../index.jsp";
    }

    @RequestMapping(value = "/user/login")
    public String studentLogin(String id, String password, HttpServletRequest request) throws ServletException, IOException {

        User currentUser = userService.getUser(id, password);
        System.out.println(currentUser);
        if ("".equals(currentUser) || currentUser == null) {
            System.out.println("用戶不存在或者密码错误");
            request.getSession().setAttribute("message", "用户名或密码错误");
            return "redirect:/index.jsp";
        } else {
            System.out.println("登录成功");
            if (currentUser.getIdentity1().equals("1")) {
                request.getSession().setAttribute("name", currentUser.getName());
                request.getSession().setAttribute("UID",currentUser.getId());
                return "../../allData";
            } if (currentUser.getIdentity1().equals("2")) {
                request.getSession().setAttribute("name", currentUser.getName());
                request.getSession().setAttribute("UID",currentUser.getId());
                return "../../getAllNotice/student";
            }
            return null;
        }
    }

    @RequestMapping(value = "/signOut")
    public String signOut(HttpServletRequest request){
        request.getSession().removeAttribute("name");
        return "../../index.jsp";
    }


    @RequestMapping(value = "/allData")
    public String getAllData(HttpServletRequest request){
        List<Student> list = studentService.showAllStudent();
        for (Student student : list) {
            System.out.println(student);
        }
        request.getSession().setAttribute("students",list);
        return "../../pages/users/data.jsp";
    }

    @RequestMapping(value = "/getData")
    public String getData(String studentId,HttpServletRequest request) throws UnsupportedEncodingException {

        Student student = studentService.checkUser(studentId, null);
        request.getSession().setAttribute("studentItem",student);
        System.out.println(student);
        return "../../pages/users/update.jsp";
    }

    @RequestMapping(value = "/addStudent")
    public String addStudent(String id,String name, String password,String sex, String major) throws UnsupportedEncodingException {
        Student student = new Student(id, name, password, sex, major);
        System.out.println(student);
        int i = studentService.addStudent(student);
        int j = userService.addUser(new User(id, student.getName(), student.getPassword(), "2"));
        if(i >0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return "../../allData";
    }

    @RequestMapping(value = "/updateData")
    public String updateData(String id,String name, String password,String sex, String major,HttpServletRequest request) throws UnsupportedEncodingException {
        String ssex = sex;
        Student student = new Student(id, name, password, ssex, major);
        System.out.println(student);
        int i = studentService.updateStudent(student);
        int j = userService.updateUser(new User(id, name, password, "2"));
        if(i >0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return "../../allData";
    }

    @RequestMapping(value = "/deleteData")
    public String deleteData(String id){
        System.out.println(id);
        int i = studentService.deleteStudent(id);
        int j = userService.deleteUser(id);
        if(i >0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return "redirect:/allData";
    }

    @RequestMapping(value = "/getAllCourses")
    public String getAllCourses(HttpServletRequest request){
        List<Course> courses = courseService.showAll();
        for (Course course : courses) {
            System.out.println(course);
        }
        request.getSession().setAttribute("courses",courses);
        return "../../pages/users/course.jsp";
    }

    @RequestMapping(value = "/addCourse")
    public String addCourse(String course,String week,String begin,String end,String major,String teacher){
        Course course1 = new Course(IDUtil.getID(),course, week, begin, end, major, teacher);
        System.out.println(course1);
        int i = courseService.addCourse(course1);
        if(i >0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return "redirect:/getAllCourses";
    }
    @RequestMapping(value = "/getCourse")
    public String getCourse(String id,HttpServletRequest request){
        Course course = courseService.getCourse(id);
        System.out.println(course);
        request.getSession().setAttribute("course",course);
        request.setAttribute("action","add");
        return "../../pages/users/addCourse.jsp";
    }

    @RequestMapping(value = "/updateCourse")
    public String updateCourse(String id, String course, String week, String begin, String end, String major, String teacher) {
        Course course1 = new Course(id, course, week, begin, end, major, teacher);
        int i = courseService.updateCourse(course1);
        if (i > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        return "redirect:/getAllCourses";
    }

    @RequestMapping(value = "/deleteCourse")
    public String deleteCourse(String id){
        System.out.println(id);
        int i = courseService.deleteCourse(id);
        if(i >0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return "redirect:/getAllCourses";
    }

    @RequestMapping(value = "/getWeather")
    public void getWeather(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        Map<Integer, Weather> allWeather = WeatherUtil.getAllWeather();
//        request.getSession().setAttribute("weathers",allWeather);
        request.getSession().setAttribute("action", "weather");
        response.sendRedirect(request.getContextPath() + "/pages/all/weather.jsp");
    }
    @RequestMapping(value = "/getWeathers")
    public void getWeathers(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        Map<Integer, Weather> allWeather = WeatherUtil.getAllWeather();
//        request.getSession().setAttribute("weathers",allWeather);
        request.getSession().setAttribute("action", "weather");
        response.sendRedirect(request.getContextPath() + "/pages/students/scourseList.jsp");
    }

    @RequestMapping(value = "/getAllNotice/{action}")
    public String getAllNotice(@PathVariable String action,HttpServletRequest request){
        List<Notice> notices = noticeService.showAllNotice();
        request.getSession().setAttribute("notices",notices);
        request.getSession().setAttribute("action",action);
        if(action.equals("admin")){
            return "../../pages/users/notice.jsp";
        }else if(action.equals("student")){
            return "../../pages/students/scourseList.jsp";
        }
        else {
            return null;
        }
    }

    @RequestMapping(value = "/deleteNotice/{id}")
    public String deleteNotice(@PathVariable String id){
        int i = noticeService.deleteNotice(id);
        if(i > 0){
            System.out.println("删除成功");
        }
        return "redirect:/getAllNotice/admin";
    }

    @RequestMapping(value = "/addNotice")
    public String addNotice(String creator,String content){
        String id = IDUtil.getID();
        int i = noticeService.addNotice(new Notice(id,content,creator));
        if(i > 0){
            System.out.println("添加成功");
        }
        return "redirect:/getAllNotice/admin";
    }

    @RequestMapping(value = "/getNotice/{id}")
    public String getNotice(@PathVariable String id,HttpServletRequest request,Model model){
        Notice notice = noticeService.getOneNotice(id);
        request.getSession().setAttribute("notice",notice);
        model.addAttribute("action", "delete");
        return "../../pages/users/addNotice.jsp";
    }

    @RequestMapping(value = "/updateNotice")
    public String updateNotice( String id, String content, String creator){
        int i = noticeService.updateNotice(new Notice(id,content,creator));
        if(i > 0){
            System.out.println("修改成功");
        }
        return "redirect:/getAllNotice/admin";
    }
}
